import Vue from 'vue'
import Vuex from 'vuex'
import home from './module/home'
import topic from './module/topic'
import category from './module/category'
import homeCategory from './module/homeCategory'

import { getGoodsInfo } from '@/api/pages/goods/index'

Vue.use(Vuex)

var openId = localStorage.getItem('openId')

export default new Vuex.Store({
    state: {
        goods: '',
        // openId,
        cardCount: 0,
        orderList: [],
        nowPosition: '郑州市',
        point: {
            lat: 34.75344,
            lng: 113.631416
        }
    },
    mutations: {
        // 操作异步获取商品详情的结果
        setGoods(state, res) {
            state.goods = res
        },
        // 点击返回的时候清除商品详情
        clrGoods(state) {
            state.goods = '';
        },
        // 设置总价格
        setCount(state, val) {
            state.cardCount = val
        },
        // 设置选中的商品
        setOrderList(state, val) {
            state.orderList = val
            console.log(state.orderList);
        },
        // 设置当前城市
        setPosition(state, item) {
            state.nowPosition = item.address;
            state.point = item.point;
        }
    },
    actions: {
        // 异步获取商品详情
        getGoods(content, id) {
            getGoodsInfo({ id, openId,})
            .then(res => {
                content.commit('setGoods', res)
            })
        }
    },
    modules: {
        home,
        topic,
        category,
        homeCategory
    }
})
